Skip to content

fix: upsert attachmemts only when attachment exist and handle FileList for react native #1539

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
May 23, 2025

Conversation

khushal87
Copy link
Member

The goal of the PR is to upsert attachments only when they exist. This is especially important because on React Native, we allow cancelling uploading by calling removeAttachments, but when the upload still happens, the upsert proceeds. We will only update you if the attachment is present.

The other fix is that the FileList is not supported on RN. Thanks to @MartinCupela for a prompt suggestion on this issue.

Copy link
Contributor

github-actions bot commented May 14, 2025

Size Change: +320 B (+0.08%)

Total Size: 381 kB

Filename Size Change
dist/cjs/index.browser.cjs 108 kB +100 B (+0.09%)
dist/cjs/index.node.cjs 152 kB +96 B (+0.06%)
dist/esm/index.js 121 kB +124 B (+0.1%)

compressed-size-action

@@ -448,7 +448,11 @@ export class AttachmentManager {
},
};

this.upsertAttachments([failedAttachment]);
const isAttachmentPresent =
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@khushal87 we could prevent duplication of these lines by adding a new method updateAttachmet to AttachmentManager. A proposal of a refactor:

private prepareAttachmentUpdate = (attachmentToUpdate: LocalAttachment) => {
    const stateAttachments = this.attachments;
    const attachments = [...this.attachments];
    const attachmentIndex = this.getAttachmentIndex(attachmentToUpdate.localMetadata.id);

    if (attachmentIndex === -1) return null;

    const merged = mergeWithDiff<LocalAttachment>(
      stateAttachments[attachmentIndex] ?? {},
      attachmentToUpdate,
    );
    const updatesOnMerge = merged.diff && Object.keys(merged.diff.children).length;
    
    if (updatesOnMerge) {
      const localAttachment = ensureIsLocalAttachment(merged.result);
      if (localAttachment) {
        attachments.splice(attachmentIndex, 1, localAttachment);
        return attachments;
      }
    }
    return null;
  };

  updateAttachment = (attachmentToUpdate: LocalAttachment) => {
    const updatedAttachments = this.prepareAttachmentUpdate(attachmentToUpdate);
    if (updatedAttachments) {
      this.state.partialNext({ attachments: updatedAttachments });
    }
  };

  upsertAttachments = (attachmentsToUpsert: LocalAttachment[]) => {
    if (!attachmentsToUpsert.length) return;
    
    let attachments = [...this.attachments];
    let hasUpdates = false;

    attachmentsToUpsert.forEach((attachment) => {
      const updatedAttachments = this.prepareAttachmentUpdate(attachment);
      if (updatedAttachments) {
        attachments = updatedAttachments;
        hasUpdates = true;
      } else {
        const localAttachment = ensureIsLocalAttachment(attachment);
        if (localAttachment) {
          attachments.push(localAttachment);
          hasUpdates = true;
        }
      }
    });

    if (hasUpdates) {
      this.state.partialNext({ attachments });
    }
  };

the in uploadAttachment you would only do this.updateAttachment(failedAttachment). WDYT?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, this can do the trick as well. My bad I was testing upsertAttachments and thought this would not work but using the updateAttachments can be useful here and avoid duplication.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have pushed the code and added a few relevant tests. Let me know if its fine to merge it. Thanks 😄

@khushal87 khushal87 merged commit a7f0e02 into master May 23, 2025
4 checks passed
@khushal87 khushal87 deleted the fix/attachment-manager-fixes branch May 23, 2025 10:03
github-actions bot pushed a commit that referenced this pull request May 26, 2025
## [9.2.0-offline-support-beta.3](v9.2.0-offline-support-beta.2...v9.2.0-offline-support-beta.3) (2025-05-26)

### Bug Fixes

* handle queueing purely offline events while connection still not marked as failed ([39c6848](39c6848))
* not needed watching and some race conditions ([75f4345](75f4345))
* reaction event enrichment but not state update ([110431e](110431e))
* take optionality into account ([5720a61](5720a61))
* upsert attachmemts only when attachment exist and handle FileList for react native ([#1539](#1539)) ([a7f0e02](a7f0e02))
@stream-ci-bot
Copy link

🎉 This PR is included in version 9.2.0-offline-support-beta.3 🎉

The release is available on:

Your semantic-release bot 📦🚀

github-actions bot pushed a commit that referenced this pull request May 27, 2025
## [9.2.0](v9.1.1...v9.2.0) (2025-05-27)

### Bug Fixes

* add new poll option only when all of the current options are filled ([#1532](#1532)) ([86cd646](86cd646))
* allow to compose non-empty message from draft without local edits ([#1533](#1533)) ([a704fc3](a704fc3))
* prevent empty poll options and questions in composition ([#1541](#1541)) ([b2e2702](b2e2702))
* respect attachment upload config for files of type FileReference ([#1545](#1545)) ([5f4b551](5f4b551))
* upsert attachmemts only when attachment exist and handle FileList for react native ([#1539](#1539)) ([a7f0e02](a7f0e02))

### Features

* [MOD-513]: Soft/Hard Delete Messages on Ban ([#1538](#1538)) ([5fff918](5fff918))
* add toJSON to ErrorFromResponse class ([#1540](#1540)) ([8f6948f](8f6948f)), closes [GetStream/chat#8211](https://github.com/GetStream/chat/issues/8211)
* offline support rework ([#1543](#1543)) ([f84d0f3](f84d0f3))

### Refactors

* general code cleanup (Threads/Polls/MessageComposer) ([#1522](#1522)) ([e4db506](e4db506))
@stream-ci-bot
Copy link

🎉 This PR is included in version 9.2.0 🎉

The release is available on:

Your semantic-release bot 📦🚀

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants